package com.luchao.back.controller;

import com.luchao.back.common.constant.Execute;
import com.luchao.back.common.constant.Validate;
import com.luchao.back.common.pojo.PageResult;
import com.luchao.back.common.pojo.ResponseResult;
import com.luchao.back.pojo.Types;
import com.luchao.back.service.TypesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "类型管理")
@RestController
@RequestMapping("/types")
public class TypesController {

    @Autowired
    private TypesService typesService;

    @ApiOperation("分页获取类型列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "当前页长度", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "5")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getTypesByPage")
    public ResponseResult getTypesByPage(Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 5 : pageSize;
        long total = typesService.getTypesCount();
        long totalPages = total % pageSize == 0 ? total / pageSize : (total / pageSize) + 1;
        pageNum = total <= pageSize ? 1 : pageNum;
        PageResult<Types> pageResult = new PageResult<Types>();
        pageResult.setPageNum(pageNum);
        pageResult.setCount(total);
        pageResult.setPage(totalPages);
        pageResult.setResult(typesService.getTypesByPage(pageNum, pageSize));
        return ResponseResult.success(pageResult);
    }

    @ApiOperation("添加类型")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "types", value = "类型对象信息", required = true, paramType = "formData", dataType = "Types", dataTypeClass = Types.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/addTypes")
    public ResponseResult addTypes(@RequestBody Types types) {
        return typesService.addTypes(types) > 0 ? ResponseResult.success(ResponseResult.SUCCESS, "新增成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "新增失败", Execute.ERROR);
    }

    @ApiOperation("修改类型信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "types", value = "类型对象信息", required = true, paramType = "formData", dataType = "Types", dataTypeClass = Types.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/editTypes")
    public ResponseResult editTypes(@RequestBody Types types) {
        return typesService.editTypes(types) > 0 ? ResponseResult.success(ResponseResult.SUCCESS, "修改成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "修改失败", Execute.ERROR);
    }

    @ApiOperation("校验类型唯一性")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "tname", value = "类型名称", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/checkTypes")
    public ResponseResult checkTypes(String tname) {
        return typesService.checkTypes(tname) > 0 ? ResponseResult.success(ResponseResult.FAILED, "类型已存在,不可以使用", Validate.ORDINARY) : ResponseResult.success(ResponseResult.SUCCESS, "类型不存在,可以使用", Validate.UNIQUE);
    }

    @ApiOperation("批量禁用/解禁类型")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "list", value = "类型信息列表", required = true, paramType = "formData", dataType = "List<Types>", dataTypeClass = List.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/batchChangeState")
    public ResponseResult batchChangeState(@RequestBody List<Types> list) {
        return typesService.batchChangeState(list) ? ResponseResult.success(ResponseResult.SUCCESS, "操作成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.ERROR, "操作失败", Execute.ERROR);
    }

    @ApiOperation("获取所有类型列表")
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getAllTypes")
    public ResponseResult getAllTypes() {
        return ResponseResult.success(typesService.getAllTypes());
    }

    @ApiOperation("获取所有前台展现的类型列表")
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getAllTypesForView")
    public ResponseResult getAllTypesForView() {
        return ResponseResult.success(typesService.getAllTypesForView());
    }
}
